$(function() {
	initPage();
});
$("#registerEventImgAdress").error(function() {
	$(this).attr("src", "../img/nopicture.png");
	$(this).error = null;
});
function showDetailRegister(data) {
	$.ajax({
		url : rootPath + "/ui/archive/showDetailRegister/" + data.registerId,
		dataType : 'json',
		type : 'GET',
		success : function(result) {
			if (result.code == 1) {
				var r = result.data.register;
				$("#registerType").val(r.registerEventType.eventName);
				$("#registerEventAccTime").val(moment(r.registerEventAccTime).format("YYYY/MM/DD HH:mm:ss"));
				$("#registerEventCreateUser").val(r.registerEventCreateUser.name);
				$("#registerEventToNetGrid").val(r.registerEventToNetGrid.gridName);
				$("#registerEventSeriousLevel").val(r.registerEventSeriousLevel.title);
				$("#registerEventEvaluateLevel").val(r.registerEventEvaluateLevel.gradingName);
				$("#registerEventAdress").val(r.registerEventAdress);
				$("#registerEventContent").val(r.registerEventContent);
				$("#registerEventMemo").val(r.registerEventMemo);
				if(r.registerEventImgAdress != ""){
					$("#registerEventImgAdress").attr("src",rootPath+"/"+r.registerEventImgAdress);
				}else{
					$("#registerEventImgAdress").attr("src","../img/nopicture.png");
				}
				$("#detailRegister").show();
			}
		}
	});
};
// 8 撤销查询
jQuery('.resetBtn').on('click', function() {
	jQuery('#search').val('');
	serverData.key = '';
	serverTable.draw();
});
// 7 点击查询按钮
$(".searchBtn").click(function() {
	var val = $("#search").val();
	serverData.key = val;
	serverTable.draw();
});
function deleteArchiveById(data) {
	var id = data.id;

	$.ajax({
		url : rootPath + "/ui/archive/operateArchive/" + id,
		dataType : 'json',
		type : 'POST',
		data : '_method=DELETE',
		success : function(result) {
			if (result.code == 1) {
				window.location.href = rootPath + "/ui/archive";
			}
		}
	});
};
var serverData = {};
var serverTable;
function initPage() {

	serverTable = $('#dataTable')
			.DataTable(
					{
						"serverSide" : true,
						"ordering" : false,
						"ajax" : {
							"dataType" : "json",
							"type" : "post",
							"cache" : false,
							"url" : window.rootPath + '/ui/archive/initPage',
							"data" : function(d) {
								return $.extend({}, d, serverData);
							}
						},
						columns : [
								{
									data : null,
									defaultContent : ""
								},
								{
									data : 'registerEventName',
									render : function(data, type, full, meta) {
										return "<a class='aClick' href='#'>"
												+ data + "</a>";
									}
								},
								{
									data : 'eventTypeName'
								},
								{
									data : 'dealUserName'
								},
								{
									data : 'memo',
								},
								{
									data : 'content'
								},
								{
									data : 'archeveDate',
									render : function(data, type, full, meta) {
										if (data)
											return moment(data).format(
													'YYYY/MM/DD HH:mm:ss');
										else
											return '';
									}
								},
								{
									data : 'photourl',
									render : function(data, type, full, meta) {
										if (data) {
											var pn = rootPath + "/" + data;
											var html = "<img width='20%' src='"
													+ pn + "'/>";
											return html;
										} else {
											return '';
										}

									}
								}

						],
						"drawCallback" : function(oSettings) {
							var k = 0;
							var total = oSettings.aiDisplay.length
									+ oSettings._iDisplayStart;
							for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
								$(
										'td:eq(0)',
										oSettings.aoData[oSettings.aiDisplay[k]].nTr)
										.text(i + 1);
								k++;
							}
							serverTable.off('click', 'tr');
							serverTable
									.on(
											'click',
											'tr',
											function(event) {
												event = event || window.event;
												var target = event.target
														|| event.scrElement;
												var data = serverTable
														.row(this).data();

												if (target.className
														.indexOf('fa-remove') != -1
														|| target.className
																.indexOf('delBtn') != -1) {
													// 2 删除
													deleteArchiveById(data);
												}

												if (target.className
														.indexOf('aClick') != -1) {
													// 3 编辑
													showDetailRegister(data);
												}

												return;
											});

						}
					});
};